//app.js
const { wxPost, wxGet, wxRunAsync, setJwt } = require('./utils/common.js')

App({
  onLaunch: function () {
  
  },
  globalData: {
    hasWxAuth: false,
    hasUserInfo: false,
    userId: '',
    subUserId:'',
    code: '',
    serverBusy: false,
    userInfo: {
      id: '',
      nickName: '',
      avatarUrl: '',
      gender: '',
      city: '',
      province: '',
      country: ''
    }
  },
  appLogin: function () {
    const that = this
    return wxRunAsync((resolve, reject) => {
      wx.login({
        success: res => {
          const code = res.code
          const { nickName, avatarUrl, gender, city, province, country } = that.globalData.userInfo
          const { userId } = that.globalData
          wxPost('/common/loginApp', { userId, code, nickName, avatarUrl, gender, city, province, country }, ({ data }) => {
            if (data.errorCode === 0) {
              wx.setStorageSync('userId', data.userInfo.id)
              that.globalData.userId = data.userInfo.id
              that.globalData.userInfo = data.userInfo
              setJwt(data.jwt)
              if (resolve) {
                resolve(data)
              }
            }
          }, () => {
            if (reject) {
              reject({})
            }
          })
        }
      })
    })
   
  }
})